"""
!/usr/bin/env python
-*- coding: utf-8 -*-
@CreateTime : 2024/7/2 10:54
@Author  :    AnimateX
@Contact :    animatex@163.com
@File    :    fast_precision_core.py
@License :    Copyright © 2024 AnimateX. All rights reserved.
@Version :    fast_precision_core_VER0.1

------------------------------------------------------------------
# @Description:

------------------------------------------------------------------
"""
import os
import numpy as np

from utils import Utils
from precision_core import RMSLPrecision


class FastPrecision(object):
    def __init__(self, cam_intrinsic: dict):
        self.fx = cam_intrinsic['fx']
        self.fy = cam_intrinsic['fy']
        self.cx = cam_intrinsic['cx']
        self.cy = cam_intrinsic['cy']
        self.width = cam_intrinsic['width']
        self.height = cam_intrinsic['height']

    def typePrecision(self,
                      env_img_path: str,
                      cam_dis: int,
                      img_w: int,
                      img_h: int,
                      roi_dict: dict,
                      base_dis=200,
                      delta=10,
                      denoise=False,
                      test_type='f-type'):
        # [01] Read depth image
        dp_img = Utils().readImage(env_img_path, img_w, img_h, data_type=np.uint16, mode='depth')

        # [02] filter
        if denoise:
            dp_img = Utils().filter_depth_image_edge(dp_img, threshold=200)

        # [03] Preprocess pointCloud
        """
            [a] crop ground
            [b] crop left wall zero region
            [c] crop ceiling
        """
        s_y = roi_dict['s_y']
        s_x = roi_dict['s_x']
        e_y = roi_dict['e_y']
        e_x = roi_dict['e_x']
        roi = dp_img[s_y:e_y, s_x:e_x]

        # update camera intrinsic
        self.cx = self.cx - s_x
        self.cy = self.cy - s_y

        # [04] gen PointCloud
        point_cloud, zero_val_cnt = RMSLPrecision().generatePointCloudByDepthData(roi,
                                                                                  self.fx,
                                                                                  self.fy,
                                                                                  self.cx,
                                                                                  self.cy,
                                                                                  cen_ratio=1)
        # print(f" [INFO] F-Type ENV, base distance: {base_dis:4.2f}mm, camera distance: {cam_dis:4.2f}mm, "
        #       f"ZeroCnt: {zero_val_cnt:4d}")

        return point_cloud, zero_val_cnt

